Unity(十六):拓展编辑器 - 自定义绘制窗口并监听、自定义GameObject预览窗口示例

自定义绘制窗口并监听

在这里插入图片描述

using UnityEditor;
using UnityEngine;

namespace Scripts_03.Editor
{
    #region EditorWindows窗口

    public class ExpandWindow : EditorWindow, IHasCustomMenu
    {
        
        public void AddItemsToMenu(GenericMenu menu)
        {
            // 禁用菜单
            menu.AddDisabledItem(new GUIContent("禁用菜单"));
            menu.AddItem(new GUIContent("菜单1"), true, () =>
            {
                Debug.Log("菜单1");
            });
            menu.AddSeparator(""); // 分割线
            menu.AddItem(new GUIContent("菜单2/菜单2-1"), false, () =>
            {
                Debug.Log("菜单2/菜单2-1");
            });
        }

        [MenuItem("Window/Open My Window")]
        static void Init()
        {
            ExpandWindow window = GetWindow<ExpandWindow>(typeof(ExpandWindow));
            window.Show();
        }

        private Texture _texture;

        private float _slider = 0.5f;

        void Awake()
        {
            Debug.Log("Awake ----> 窗口初始化时调用");
            _texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity.png");
        }

        void OnGUI()
        {
            // Debug.Log("绘制窗口");
            GUILayout.Label("Hello World!!", EditorStyles.boldLabel);
            var guiStyle = new GUIStyle();
            guiStyle.fixedWidth = 100;
            guiStyle.fixedHeight = 100;
            GUILayout.Box(_texture, guiStyle);
            _slider = EditorGUILayout.Slider("Slider", _slider, -10, 10);
        }

        void OnFocus()
        {
            Debug.Log("OnFocus ----> 拥有焦点");
        }

        void OnLostFocus()
        {
            Debug.Log("OnLostFocus ----> 失去焦点");
        }

        void OnSelectionChange()
        {
            Debug.Log("OnSelectionChange ----> 在 Hierarchy 或者 Project 视图中选择一个对象时调用");
        }

        void OnHierarchyChange()
        {
            Debug.Log("OnHierarchyChange ----> Hierarchy 视图发生改变时调用");
        }

        void OnProjectChange()
        {
            Debug.Log("OnProjectChange ----> Project 视图发生改变时调用");
        }

        void Update()
        {
            // Debug.Log("Update ----> 每帧更新");
        }

        void OnInspectorUpdate()
        {
            // Debug.Log("OnInspectorUpdate ----> Inspector 每帧更新");
        }

        void OnDestroy()
        {
            Debug.Log("OnDestroy ----> 窗口销毁时调用");
        }

    }

    #endregion

    
}

自定义GameObject预览窗口示例

在这里插入图片描述

using UnityEditor;
using UnityEngine;

public class GameObjPreview : EditorWindow
{
    private GameObject _gameObject;

    private Editor _editor;

    [MenuItem("Window/GameObject预览")]
    public static void ObjPreview()
    {
        GameObjPreview window = GetWindow<GameObjPreview>(typeof(GameObjPreview));
        window.Show();
    }

    private void OnGUI()
    {
        GUILayout.Label("Hello GameObject!!!");
        var gameObject = (GameObject)EditorGUILayout.ObjectField(_gameObject, typeof(GameObject), true);
        // 每次gameObject改变时都实例化_editor(gameObject != _gameObject ---> 比较当前GameObject与上一次的区别)
        if (gameObject != _gameObject)
        {
            _gameObject = gameObject; 
            _editor = Editor.CreateEditor(_gameObject);
        }
        if (_gameObject != null)
            _editor.OnPreviewGUI(GUILayoutUtility.GetRect(300, 300), EditorStyles.whiteLabel);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prosper Lee

您的赏赐将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值